package com.tjrac.generalpermissions.service.impl;

import com.tjrac.filter.stereotype.PermissionControl;
import com.tjrac.generalpermissions.dao.PermissionDao;
import com.tjrac.generalpermissions.domain.Permission;
import com.tjrac.generalpermissions.epception.MsgException;
import com.tjrac.generalpermissions.epception.statusCode.PermissionCode;
import com.tjrac.generalpermissions.service.PermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ZeNing
 * @create 2020-11-27 20:27
 */
@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Resource
    PermissionDao permissionDao;

    @PermissionControl("查看权限")
    @Override
    public Permission queryById(int id) throws MsgException {
        Permission permission = permissionDao.queryById(id);
        if (permission == null) {
            throw new MsgException(PermissionCode.NOT_EXIST_PERMISSION, "该权限不存在,查看该权限失败");
        } else {
            return permission;
        }
    }

    @PermissionControl("查看所有权限")
    @Override
    public String queryAllPermission() throws MsgException {
        List<Permission> permissions = permissionDao.queryAll();
        if (permissions == null) {
            throw new MsgException(PermissionCode.FAILURE_SELECT_ALL, "查看所有权限失败");
        } else {
            return new MsgException(PermissionCode.SUCCESS_PERMISSION, "查看所有权限成功").toListJson(permissions);
        }
    }

    @Override
    @PermissionControl("添加权限")
    public String addPermission(Permission permission) throws MsgException {
        if (permissionDao.queryByName(permission.getName()) != null) {
            throw new MsgException(PermissionCode.EXIST_PERMISSION, "权限已存在!");
        } else {
            if (permissionDao.addPermission(permission) != 0) {
                return new MsgException(PermissionCode.SUCCESS_PERMISSION, "权限添加成功").toJson();
            } else {
                throw new MsgException(PermissionCode.FAILURE_ADD, "权限添加失败");
            }
        }
    }

    @PermissionControl("删除权限")
    @Override
    public String delPermission(int id) throws MsgException {
        if (permissionDao.queryById(id) == null) {
            throw new MsgException(PermissionCode.NOT_EXIST_PERMISSION, "权限不存在");
        } else {
            if (permissionDao.delById(id) != 0) {
                return new MsgException(PermissionCode.SUCCESS_PERMISSION, "权限删除成功").toJson();
            } else {
                throw new MsgException(PermissionCode.FAILURE_DEL, "权限删除失败");
            }
        }
    }

    @PermissionControl("修改权限")
    @Override
    public String modifyPermission(Permission permission) throws MsgException {
        if (permissionDao.queryById(permission.getId()) == null) {
            throw new MsgException(PermissionCode.NOT_EXIST_PERMISSION, "权限不存在,无法修改");
        } else {
            if (permissionDao.modifyById(permission) != 0) {
                return new MsgException(PermissionCode.SUCCESS_PERMISSION, "权限修改成功").toJson();
            } else {
                throw new MsgException(PermissionCode.FAILURE_UPD, "权限修改失败");
            }
        }
    }

}
